package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Dengji;
import com.hopu.pojo.House;
import com.hopu.service.HouseService;
import com.hopu.util.FileUtil;
import com.hopu.util.ResultUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("house")
public class HouseController {

    @Resource
    private HouseService houseServiceImpl;

    /**
     * 查询房屋信息 分页
     * @param pn
     * @return
     */
    @RequestMapping("findAllHousePD")
    private ResultUtils findAllHousePD(
            @RequestParam(value = "pn",defaultValue = "1")Integer pn,
            @RequestParam(value = "hflag",defaultValue = "0")Integer hflag,
            @RequestParam(name = "pageSize",defaultValue = "14")Integer pageSize){
        PageHelper.startPage(pn,pageSize);
        List<House> list = houseServiceImpl.findAllHousePD(hflag);
        PageInfo page = new PageInfo(list);
        return  new ResultUtils(200,"SUCCESS",page);
    }

    /**
     * 添加房子信息
     * @param sid
     * @param aid
     * @param haddress
     * @param hfh
     * @param hhx
     * @param hmj
     * @param hcx
     * @param hmoney
     * @param hwf
     * @param hdx
     * @param hsf
     * @param hmq
     * @param dkd
     * @param skd
     * @param mkd
     * @param hjp
     * @param hremark
     * @return
     */
    @PostMapping("addHouse")
    private  ResultUtils addHouse(
            MultipartHttpServletRequest multipartRequest, HttpServletResponse response,
            @RequestParam(value = "sid",required = true)Integer sid, @RequestParam(value = "aid",required = true)Integer aid,
            @RequestParam(value = "haddress",required = true)String haddress, @RequestParam(value = "hfh",required = true)String hfh,
            @RequestParam(value = "hhx",required = true)String hhx, @RequestParam(value = "hmj",required = true)String hmj,
            @RequestParam(value = "hcx",required = true)String hcx, @RequestParam(value = "hmoney",required = true)float hmoney,
            @RequestParam(value = "hwf",required = true)float hwf, @RequestParam(value = "hdx",required = true)float hdx,
            @RequestParam(value = "hsf",required = true)float hsf, @RequestParam(value = "hmq",required = true)float hmq,
            @RequestParam(value = "dkd",required = true)float dkd, @RequestParam(value = "skd",required = true)float skd,
            @RequestParam(value = "mkd",required = true)float mkd, @RequestParam(value = "hjp",required = true)String hjp,
            @RequestParam(value = "hremark",required = true)String hremark) throws IOException {
        String path = "";
        try {
            Iterator<String> it = multipartRequest.getFileNames();
            while (it.hasNext()){
                String filePath = FileUtil.upload(multipartRequest.getFile((String) it.next()));
                if (it.hasNext()) {
                    path+=filePath+"、";
                }
            }
        } catch (Exception e) {}

         path = path.substring(0, path.length() - 1);

        int num =  houseServiceImpl.addHouse(sid,aid,haddress,hfh,hhx,hmj,hcx,hmoney,hwf,hdx,hsf,hmq,dkd,skd,mkd,hjp,hremark,path);
        if (num>0){
            return new ResultUtils(200,"SUCCESS",null);
        }
        return new ResultUtils(400,"ERROR",null);
    }
    /**
     * 修改房子信息
     * @param sid
     * @param aid
     * @param haddress
     * @param hfh
     * @param hhx
     * @param hmj
     * @param hcx
     * @param hmoney
     * @param hwf
     * @param hdx
     * @param hsf
     * @param hmq
     * @param dkd
     * @param skd
     * @param mkd
     * @param hjp
     * @param hremark
     * @return
     */
    @PostMapping("uppHouse")
    private ResultUtils uppHouse(@RequestParam Integer hid, @RequestParam(value = "sid",required = true)Integer sid, @RequestParam(value = "aid",required = true)Integer aid,
                                 @RequestParam(value = "haddress",required = true)String haddress, @RequestParam(value = "hfh",required = true)String hfh,
                                 @RequestParam(value = "hhx",required = true)String hhx, @RequestParam(value = "hmj",required = true)String hmj,
                                 @RequestParam(value = "hcx",required = true)String hcx, @RequestParam(value = "hmoney",required = true)float hmoney,
                                 @RequestParam(value = "hwf",required = true)float hwf, @RequestParam(value = "hdx",required = true)float hdx,
                                 @RequestParam(value = "hsf",required = true)float hsf, @RequestParam(value = "hmq",required = true)float hmq,
                                 @RequestParam(value = "dkd",required = true)float dkd, @RequestParam(value = "skd",required = true)float skd,
                                 @RequestParam(value = "mkd",required = true)float mkd, @RequestParam(value = "hjp",required = true)String hjp,
                                 @RequestParam(value = "hremark",required = true)String hremark) {

        int num =  houseServiceImpl.uppHouse(hid,sid,aid,haddress,hfh,hhx,hmj,hcx,hmoney,hwf,hdx,hsf,hmq,dkd,skd,mkd,hjp,hremark);
        if (num>0){
            return new ResultUtils(200,"SUCCESS",null);
        }
        return new ResultUtils(400,"ERROR",null);
    }

    /**
     * 根据id或者name查找房子名称
     * @param hid
     * @param hid
     * @return
     */
    @PostMapping("findHouseById")
    private ResultUtils findHouseById(@RequestParam(name = "hid") Integer hid){
        House house = houseServiceImpl.findHouseById(hid);
        if (!ObjectUtils.isEmpty(house)) {
            return new ResultUtils(200,"SUCCESS",house);
        }
        return new ResultUtils(400,"ERROR",house);
    }

    /**
     * 下架房屋
     * @param hid
     * @return
     */
    @PostMapping("delHouse")
    private ResultUtils delHouse(@RequestParam(value = "hid",required = true)Integer hid){
        int num = houseServiceImpl.delHouse(hid);
        return new ResultUtils(200,"SUCCESS",1);
    }

    /**
     * 恢复房屋
     * @param hid
     * @return
     */
    @PostMapping("huiFuHouse")
    private ResultUtils huiFuHouse(@RequestParam(value = "hid",required = true) Integer hid){
        System.out.println(hid);
        int num = houseServiceImpl.huiFuHouse(hid);
        return new ResultUtils(200,"SUCCESS",1);
    }
    @PostMapping("findHouseByAid")
    private ResultUtils findHouseByAid(@RequestParam(value = "aid",required = true) Integer aid) {
        System.out.println(aid);
        List<House> houses = houseServiceImpl.findHouseByAid(aid);
        if (houses.size()>0) {
            return new ResultUtils(200,"SUCCESS",houses);
        }
        House house = new House();
        house.setHaddress("此地区无房可租");
        house.setHfh("(╥╯^╰╥)");
        return new ResultUtils(9999,"ERROR",house);
    }
    @PostMapping("findAllHouseByCondition")
    public ResultUtils findAllHouseByCondition(House house, @RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<House> allHouse = houseServiceImpl.findAllHouseByCondition(house);

            PageInfo pageInfo = new PageInfo(allHouse);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }

    }
    @PostMapping("findAllHouseByAidAndSid")
    public ResultUtils findAllHouseByAidAndSid(House house){

            List<House> allHouse = houseServiceImpl.findAllHouseByAidAndSid(house);
            return new ResultUtils(200,"查询单条数据成功",allHouse);

    }
 }

